package com.itfreer.power;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;

import com.itfreer.form.api.PowerReplace;
import com.itfreer.power.entity.user.VUserInfoEntity;

/**
 * 定义系统过滤器
 * ${userid}当前用户ID，
 * ${orgid}当前用户所在机构，
 * ${deptid}当前用户所在部门
 */
@Component("SysPowerReplace")
public class SysPowerReplace implements PowerReplace {
	
	private final static String s_user = "s_user";
	
	@Override
	public String replace(HttpSession httpSession, String item) {
		VUserInfoEntity userEntity = httpSession != null ? (VUserInfoEntity) httpSession.getAttribute(s_user) : null;
		if (userEntity != null) {
						
			String userid = userEntity.getUserid();
			if(null == userid) {
				item = item.replace("${userid}","");
			}else {
				item = item.replace("${userid}",userid);
			}
			
			String orgid = userEntity.getOrganizationId();
			if(null == orgid) {
				item = item.replace("${orgid}","");
			}else {
				item = item.replace("${orgid}",orgid);
			}
			
			String deptid = userEntity.getDepartmentId();
			if(null == deptid) {
				item = item.replace("${deptid}","");
			}else {
				item = item.replace("${deptid}",deptid);
			}
		}
		return item;
	}
	
}
